/**
 * Created by Elsa on 2017/8/29.
 */
layui.define(['form','jquery'], function(exports){
    var form = layui.form(),
        $ = layui.jquery,
        layer = layui.layer;

    var rgstnum = getQueryString("rgstnum");
    console.log(rgstnum);

    $.ajax({
        type:"GET",
        url:contextPath+"/project/ops/lj/"+ rgstnum ,
        contentType: "application/json; charset=utf-8",
        dataType:"json",
        success:function (data) {
            console.log(data);
            $('input[name="shipname"]').attr("value",data.shipname);
            $('input[name="rgstnum"]').attr("value",data.rgstnum);


            if(data.ljdate){
                $('input[name="ljname"]').attr("value",data.ljname);
                $('input[name="ljdate"]').attr("value",data.ljdate);

                $('#span1').html(data.ljt1jg);
                $('input[name="ljt1jg"][value="'+data.ljt1jg+'"]').attr("checked",true);
                $('#span1x').html(data.ljt1bz);
                $('input[name="re1"]').attr("value",data.ljt1bz);

                $('#span2').html(data.ljt2jg);
                $('input[name="ljt2jg"][value="'+data.ljt2jg+'"]').attr("checked",true);
                $('#span2x').html(data.ljt2bz);
                $('input[name="re2"]').attr("value",data.ljt2bz);

                $('#span3').html(data.ljt3jg);
                $('input[name="ljt3jg"][value="'+data.ljt3jg+'"]').attr("checked",true);
                $('#span3x').html(data.ljt3bz);
                $('input[name="re3"]').attr("value",data.ljt3bz);

                $('#span4').html(data.ljt4jg);
                $('input[name="ljt4jg"][value="'+data.ljt4jg+'"]').attr("checked",true);
                $('#span4x').html(data.ljt4bz);
                $('input[name="re4"]').attr("value",data.ljt4bz);

                $('#span5').html(data.ljt5jg);
                $('input[name="ljt5jg"][value="'+data.ljt5jg+'"]').attr("checked",true);
                $('#span5x').html(data.ljt5bz);
                $('input[name="re5"]').attr("value",data.ljt5bz);

                $('#span6').html(data.ljt6jg);
                $('input[name="ljt6jg"][value="'+data.ljt6jg+'"]').attr("checked",true);
                $('#span6x').html(data.ljt6bz);
                $('input[name="re6"]').attr("value",data.ljt6bz);

                $('#span7').html(data.ljt7jg);
                $('input[name="ljt7jg"][value="'+data.ljt7jg+'"]').attr("checked",true);
                $('#span7x').html(data.ljt7bz);
                $('input[name="re7"]').attr("value",data.ljt7bz);

                $('#span8').html(data.ljt8jg);
                $('input[name="ljt8jg"][value="'+data.ljt8jg+'"]').attr("checked",true);
                $('#span8x').html(data.ljt8bz);
                $('input[name="re8"]').attr("value",data.ljt8bz);

                $('#span9').html(data.ljt9jg);
                $('input[name="ljt9jg"][value="'+data.ljt9jg+'"]').attr("checked",true);
                $('#span9x').html(data.ljt9bz);
                $('input[name="re9"]').attr("value",data.ljt9bz);

                $('#span10').html(data.ljt10jg);
                $('input[name="ljt10jg"][value="'+data.ljt10jg+'"]').attr("checked",true);
                $('#span10x').html(data.ljt10bz);
                $('input[name="re10"]').attr("value",data.ljt10bz);

                $('#span11').html(data.ljt11jg);
                $('input[name="ljt11jg"][value="'+data.ljt11jg+'"]').attr("checked",true);
                $('#span11x').html(data.ljt11bz);
                $('input[name="re11"]').attr("value",data.ljt11bz);

                $('#span12').html(data.ljt12jg);
                $('input[name="ljt12jg"][value="'+data.ljt12jg+'"]').attr("checked",true);
                $('#span12x').html(data.ljt12bz);
                $('input[name="re12"]').attr("value",data.ljt12bz);

                $.ajax({
                    type:"GET",
                    url:contextPath+"/personal/role",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        if(data.role=="管理员"){
                            $('#edit').show();
                        }else if(data.role=="验船师"){
                            $.ajax({
                                type: "GET",
                                url: contextPath + "/project/ops/" + rgstnum,
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function (data) {
                                    if(data.allstate!="完成"){
                                       $('#edit').show(); 
                                    }
                                },
                                error: function (e) {
                                }
                            });
                        }
                    },
                     error: function (e) {

                    }
                });

                $('#sus').show();
                $('#su').on('click',function () {
                    window.open('../../../print/opera/ExperRecord/lj/OperaLjRecord.html?rgstnum='+rgstnum);
                    return false;
                });

                $('#edit').on('click',function () {
                    $('#sus').hide();
                    $('#sss').show();

                    $('span.ljstate1').hide();
                    $('div.ljstate2').show();
                    $('input.ljstate2').show();

                    $('input[name="ljname"]').attr('disabled', false);
                    $('input[name="ljdate"]').attr('disabled', false);

                    $('input[name="ljname"]').attr('class', 'layui-input');
                    $('input[name="ljdate"]').attr('class', 'layui-input');

                    return false;

                });

                $('#submit').on('click',function () {
                    $(this).attr('disabled','disabled');
                    var ss = new Object();

                    ss.rgstnum = $('input[name="rgstnum"]').val();
                    ss.ljname = $('input[name="ljname"]').val();

                    if($('input[name="ljt1jg"]:checked').val()){
                        ss.ljt1jg = $('input[name="ljt1jg"]:checked').val();
                    } else {
                        ss.ljt1jg = "";
                    }
                    if($('input[name="ljt2jg"]:checked').val()){
                        ss.ljt2jg = $('input[name="ljt2jg"]:checked').val();
                    } else {
                        ss.ljt2jg = "";
                    }
                    if($('input[name="ljt3jg"]:checked').val()){
                        ss.ljt3jg = $('input[name="ljt3jg"]:checked').val();
                    } else {
                        ss.ljt3jg = "";
                    }
                    if($('input[name="ljt4jg"]:checked').val()){
                        ss.ljt4jg = $('input[name="ljt4jg"]:checked').val();
                    } else {
                        ss.ljt4jg = "";
                    }
                    if($('input[name="ljt5jg"]:checked').val()){
                        ss.ljt5jg = $('input[name="ljt5jg"]:checked').val();
                    } else {
                        ss.ljt5jg = "";
                    }
                    if($('input[name="ljt6jg"]:checked').val()){
                        ss.ljt6jg = $('input[name="ljt6jg"]:checked').val();
                    } else {
                        ss.ljt6jg = "";
                    }
                    if($('input[name="ljt7jg"]:checked').val()){
                        ss.ljt7jg = $('input[name="ljt7jg"]:checked').val();
                    } else {
                        ss.ljt7jg = "";
                    }
                    if($('input[name="ljt8jg"]:checked').val()){
                        ss.ljt8jg = $('input[name="ljt8jg"]:checked').val();
                    } else {
                        ss.ljt8jg = "";
                    }
                    if($('input[name="ljt9jg"]:checked').val()){
                        ss.ljt9jg = $('input[name="ljt9jg"]:checked').val();
                    } else {
                        ss.ljt9jg = "";
                    }
                    if($('input[name="ljt10jg"]:checked').val()){
                        ss.ljt10jg = $('input[name="ljt10jg"]:checked').val();
                    } else {
                        ss.ljt10jg = "";
                    }
                    if($('input[name="ljt11jg"]:checked').val()){
                        ss.ljt11jg = $('input[name="ljt11jg"]:checked').val();
                    } else {
                        ss.ljt11jg = "";
                    }
                    if($('input[name="ljt12jg"]:checked').val()){
                        ss.ljt12jg = $('input[name="ljt12jg"]:checked').val();
                    } else {
                        ss.ljt12jg = "";
                    }
                    ss.ljt1bz = $('input[name="re1"]').val();
                    ss.ljt2bz = $('input[name="re2"]').val();
                    ss.ljt3bz = $('input[name="re3"]').val();
                    ss.ljt4bz = $('input[name="re4"]').val();
                    ss.ljt5bz = $('input[name="re5"]').val();
                    ss.ljt6bz = $('input[name="re6"]').val();
                    ss.ljt7bz = $('input[name="re7"]').val();
                    ss.ljt8bz = $('input[name="re8"]').val();
                    ss.ljt9bz = $('input[name="re9"]').val();
                    ss.ljt10bz = $('input[name="re10"]').val();
                    ss.ljt11bz = $('input[name="re11"]').val();
                    ss.ljt12bz = $('input[name="re12"]').val();

                    console.log(JSON.stringify(ss));

                    $.ajax({
                        type:"PUT",
                        url:contextPath+"/project/ops/lj/update",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(ss),
                        dataType:"json",
                        success:function (data) {
                            console.log(data.message);
                            if(data.message == "更新轮机设备效益试验记录表成功!"){
                                layer.msg('轮机设备效益试验记录表更新成功!<br>页面将在1秒后刷新...');
                                setTimeout(function(){window.location.reload();},1000);
                            } else {
                                $('#submit').attr('disabled',false);
                                layer.msg(data.message, {
                                    time:false,
                                    btn:"确定",
                                    btnAlign: 'c'
                                });
                                $("#all-info").html("*"+data.message);
                            }
                        },
                        error:function (e) {
                        }
                    });
                    return false;

                });

                $('#refresh').on('click',function () {
                    $('#sus').show();
                    $('#sss').hide();

                    $('span.ljstate1').show();
                    $('div.ljstate2').hide();
                    $('input.ljstate2').hide();

                    $('input[name="ljname"]').attr('disabled', true);
                    $('input[name="ljdate"]').attr('disabled', true);

                    $('input[name="ljname"]').attr('class', 'layui-input layui-disabled');
                    $('input[name="ljdate"]').attr('class', 'layui-input layui-disabled');

                    return false;

                });

            } else {
                $.ajax({
                    type:"GET",
                    url:contextPath+"/personal/role",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        console.log(data.role);
                        if(data.role=="管理员"||data.role=="验船师"){
                            $('span.ljstate1').hide();
                            $('div.ljstate2').show();
                            $('input.ljstate2').show();
                            $('div#submit').show();
                            $.ajax({
                                type: "GET",
                                url: contextPath + "/personal/info",
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function (data) {
                                    $('input[name="ljname"]').attr("value",data.name);
                                },
                                error: function (e) {

                                }
                            });
                            $('input[name="ljdate"]').attr("value",getNowFormatDate());
                        }else if(data.role=="船东"||data.role=="船厂"){
                            $('span.ljstate1').hide();
                        }else{

                        }
                    },
                    error: function (e) {
                    }
                });
            }

        },
        error:function (e) {
        }
    });

    form.render();

    //自定义验证规则
    form.verify({
        title: function(value) {
            if(value.length < 5) {
                return '标题至少得5个字符啊';
            }
        },
        pass: [/(.+){6,12}$/, '密码必须6到12位'],
        content: function(value) {
            layedit.sync(editIndex);
        }
    });


    //监听提交
    form.on('submit(caseglyx)', function(data) {
        $(this).attr('disabled','disabled');
        var OperaLjRecord = new Object();
        OperaLjRecord.rgstnum = data.field.rgstnum;
        OperaLjRecord.ljname = data.field.ljname;

        if($('input[name="ljt1jg"]:checked').val()){
            OperaLjRecord.ljt1jg = $('input[name="ljt1jg"]:checked').val();
        } else {
            OperaLjRecord.ljt1jg = "";
        }
        if($('input[name="ljt2jg"]:checked').val()){
            OperaLjRecord.ljt2jg = $('input[name="ljt2jg"]:checked').val();
        } else {
            OperaLjRecord.ljt2jg = "";
        }
        if($('input[name="ljt3jg"]:checked').val()){
            OperaLjRecord.ljt3jg = $('input[name="ljt3jg"]:checked').val();
        } else {
            OperaLjRecord.ljt3jg = "";
        }
        if($('input[name="ljt4jg"]:checked').val()){
            OperaLjRecord.ljt4jg = $('input[name="ljt4jg"]:checked').val();
        } else {
            OperaLjRecord.ljt4jg = "";
        }
        if($('input[name="ljt5jg"]:checked').val()){
            OperaLjRecord.ljt5jg = $('input[name="ljt5jg"]:checked').val();
        } else {
            OperaLjRecord.ljt5jg = "";
        }
        if($('input[name="ljt6jg"]:checked').val()){
            OperaLjRecord.ljt6jg = $('input[name="ljt6jg"]:checked').val();
        } else {
            OperaLjRecord.ljt6jg = "";
        }
        if($('input[name="ljt7jg"]:checked').val()){
            OperaLjRecord.ljt7jg = $('input[name="ljt7jg"]:checked').val();
        } else {
            OperaLjRecord.ljt7jg = "";
        }
        if($('input[name="ljt8jg"]:checked').val()){
            OperaLjRecord.ljt8jg = $('input[name="ljt8jg"]:checked').val();
        } else {
            OperaLjRecord.ljt8jg = "";
        }
        if($('input[name="ljt9jg"]:checked').val()){
            OperaLjRecord.ljt9jg = $('input[name="ljt9jg"]:checked').val();
        } else {
            OperaLjRecord.ljt9jg = "";
        }
        if($('input[name="ljt10jg"]:checked').val()){
            OperaLjRecord.ljt10jg = $('input[name="ljt10jg"]:checked').val();
        } else {
            OperaLjRecord.ljt10jg = "";
        }
        if($('input[name="ljt11jg"]:checked').val()){
            OperaLjRecord.ljt11jg = $('input[name="ljt11jg"]:checked').val();
        } else {
            OperaLjRecord.ljt11jg = "";
        }
        if($('input[name="ljt12jg"]:checked').val()){
            OperaLjRecord.ljt12jg = $('input[name="ljt12jg"]:checked').val();
        } else {
            OperaLjRecord.ljt12jg = "";
        }
        OperaLjRecord.ljt1bz = data.field.re1;
        OperaLjRecord.ljt2bz = data.field.re1;
        OperaLjRecord.ljt3bz = data.field.re3;
        OperaLjRecord.ljt4bz = data.field.re4;
        OperaLjRecord.ljt5bz = data.field.re5;
        OperaLjRecord.ljt6bz = data.field.re6;
        OperaLjRecord.ljt7bz = data.field.re7;
        OperaLjRecord.ljt8bz = data.field.re8;
        OperaLjRecord.ljt9bz = data.field.re9;
        OperaLjRecord.ljt10bz = data.field.re10;
        OperaLjRecord.ljt11bz = data.field.re11;
        OperaLjRecord.ljt12bz = data.field.re12;

        console.log(JSON.stringify(OperaLjRecord));

        $.ajax({
            type:"POST",
            url:contextPath+"/project/ops/lj/new",
            contentType: "application/json; charset=utf-8",
            data:JSON.stringify(OperaLjRecord),
            dataType:"json",
            success:function (data) {
                console.log(data.message);
                if(data.message == "轮机设备效益试验记录表创建成功!"){
                    parent.layer.msg('轮机设备效益试验记录表创建成功!<br>页面将在1秒后刷新...');
                    setTimeout(function(){window.location.reload();},1000);
                } else {
                    $('#suresubmitx').attr('disabled',false);
                    layer.msg(data.message, {
                        time:false,
                        btn:"确定",
                        btnAlign: 'c'
                    });
                }
            },
            error:function (e) {
            }
        });
        return false;
    });

    exports('OperaLjRecord', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});